/**
 * 
 */

$(document).ready(function() {
	
	$('.delete').click(function(){
		var m_this = $(this);
		$.get(
			"admin/deleteComm",
			{"id":$(this).parent().parent().attr("id")},
			function(result){
				if(result)
					m_this.parent().parent().remove();
			}
		)
	});
	$('.edit').click(function(){
		window.location.href="initAdminEditComm?id="+$(this).parent().parent().attr("id");
	});
	$('#add').click(function(){
		window.location.href="initAdminEditComm?id=";
	});
	
	//类别的级联效果
	$.get(
		"admin/initTypePage",
		function(result){			
			var bid = $("#bigType").attr("class");
			$("#bigType").append("<option id='0'>---请选择---</option>");
			$("#littleType").append("<option id='0'>---请选择---</option>");
			for(var i=0; i<result.length; i++){
				if(result[i].id == bid){
					$("#bigType").append("<option selected='selected' id="+ result[i].id + ">"+ result[i].name +"</option>");

					var lid = $("#littleType").attr("class");
					$.get(
							"admin/bigType",
							{"id":bid},
							function(result){
								$("#littleType").empty(); 
								$("#littleType").append("<option selected='selected' id='0'>---请选择---</option>");
								for(var i=0; i<result.length; i++){

									if(result[i].id == lid){
										$("#littleType").append("<option selected='selected' id="+ result[i].id + ">"+ result[i].name +"</option>");
									}else 
										$("#littleType").append("<option id="+ result[i].id + ">"+ result[i].name +"</option>");
								}
							}
						)
				}else 
					$("#bigType").append("<option id="+ result[i].id + ">"+ result[i].name +"</option>");
			}
		}
	)
	
	$("body").on("change","#bigType",function(){
		var id = $('#bigType  option:selected').attr("id");
		if(id == 0){
			$("#littleType").empty(); 
			$("#littleType").append("<option id='0'>---请选择---</option>");
			return ;
		}
			$.get(
				"admin/bigType",
				{"id":id},
				function(result){
					$("#littleType").empty(); 
					$("#littleType").append("<option selected='selected' id='0'>---请选择---</option>");
					for(var i=0; i<result.length; i++){
						$("#littleType").append("<option id="+ result[i].id + ">"+ result[i].name +"</option>");
				
					}
				}
			)
	});
	var types;
	var bigType;
	var bigType2;
	var littleType;
	var keyWord;
	var minPrice;
	var maxPrice;
	
	init();
	function init(){
		types = $("#bigType").attr("id");
		bigType = $("#bigType option:selected").attr("id");
		bigType2 = $("#bigType option:selected").val();
		littleType = $("#littleType option:selected").attr("id");
		if(littleType == null || littleType == ''){
			littleType = 0;
		}
		keyWord = $("#keyWord").val();
		minPrice = $("#minPrice").val();
		maxPrice = $("#maxPrice").val();
	};
	//搜索查询
	$("#search").click(function(){
		bigType = $("#bigType option:selected").attr("id");
		littleType = $("#littleType option:selected").attr("id");
		var pageSize = $('#pageSize').val();
		if(littleType == null || littleType == ''){
			littleType = 0;
		}
		init();
		window.location.href="jumpAdminCommPage?bigType="+bigType+"&littleType="+littleType+"&keyWord="+keyWord
		+"&minPrice="+minPrice+"&maxPrice="+maxPrice+"&pageSize="+pageSize+"&currentPage=0";
	})
	
	//点击下一页
	$("#nextPage").click(function(){ 
		init();
		var pageSize = $('#pageSize').val();
		var currentPage = $('#currentPage').html();
		var totalPage = $('#totalPage').html();
		if(currentPage == totalPage){
			return ;
		}
		window.location.href="jumpAdminCommPage?bigType="+bigType+"&littleType="+littleType+"&keyWord="+keyWord
		+"&minPrice="+minPrice+"&maxPrice="+maxPrice+"&pageSize="+pageSize+"&currentPage="+currentPage;
    });  
	//点击上一页
	$("#parentPage").click(function(){ 
		init();
		var pageSize = $('#pageSize').val();
		var currentPage = $('#currentPage').html();
		if(currentPage == 1){
			return ;
		}
		window.location.href="jumpAdminCommPage?bigType="+bigType+"&littleType="+littleType+"&keyWord="+keyWord
		+"&minPrice="+minPrice+"&maxPrice="+maxPrice+"&pageSize="+pageSize+"&currentPage="+(currentPage-2);
    }); 
	//点击首页
	$("#firstPage").click(function(){ 
		init();
		var pageSize = $('#pageSize').val();
		window.location.href="jumpAdminCommPage?bigType="+bigType+"&littleType="+littleType+"&keyWord="+keyWord
		+"&minPrice="+minPrice+"&maxPrice="+maxPrice+"&pageSize="+pageSize+"&currentPage=0";
    }); 
	
	//点击尾页
	$("#lastPage").click(function(){ 
		init();
		var pageSize = $('#pageSize').val();
		var totalPage = $('#totalPage').html();
		window.location.href="jumpAdminCommPage?bigType="+bigType+"&littleType="+littleType+"&keyWord="+keyWord
		+"&minPrice="+minPrice+"&maxPrice="+maxPrice+"&pageSize="+pageSize+"&currentPage="+(totalPage-1);
    }); 
	
	//页面大小
	$("#pageSize").change(function(){ 
		init();
		var pageSize = $('#pageSize').val();
		window.location.href="jumpAdminCommPage?bigType="+bigType+"&littleType="+littleType+"&keyWord="+keyWord
		+"&minPrice="+minPrice+"&maxPrice="+maxPrice+"&pageSize="+pageSize+"&currentPage=0";
	});
	
	//跳转
	$("#selectPage").change(function(){ 
		init();
		var pageSize = $('#pageSize').val();
		var jumpPage = $("#selectPage option:selected").val();
		window.location.href="jumpAdminCommPage?bigType="+bigType+"&littleType="+littleType+"&keyWord="+keyWord
		+"&minPrice="+minPrice+"&maxPrice="+maxPrice+"&pageSize="+pageSize+"&currentPage="+(jumpPage-1);
	});
})